<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>简单图片轮播</title>
	<link rel="stylesheet" href="../lib/reset.css">
	<style>
		body {
			background-color: #ccc;
		}

		.img-slider {
			width: 500px;
			height: 200px;
			background-color: #fff;
			margin: 20px auto;
			overflow: hidden;
			position: relative;
		}

		.img-slider ul li {
			width: 500px;
			height: 200px;
			float: left;
		}

		.img-slider a {
			position: absolute;
			top: 50%;
			left: 0;
			width: 50px;
			height: 50px;
			background-color: #333;
			margin-top: -25px;
			opacity: 0.5;
			filter: alpha(opacity=50);
			display: none;
		}

		.img-slider a.next {
			left: auto;
			right: 0;
		}

		.img-slider:hover a {
			display: block;
		}

		.img-slider ol {
			width: 96px;
			height: 10px;
			background-color: red;
			position: absolute;
			bottom: 20px;
			left: 50%;
			margin-left: -48px;
			padding: 2px 1px;
    		border-radius: 7px;
		}

		.img-slider ol li {
			width: 10px;
			height: 10px;
			background-color: #ccc;
			float: left;
			margin: 0 3px;
			border-radius: 5px;
		}

		.img-slider ol li.active {
			background-color: green;
		}
	</style>
	<script src="../lib/jquery-1.12.1.min.js"></script>
	<script>
		$(document).ready(function() {
			var $slider = $('.img-slider'),
				$ul = $('.img-slider > ul'), // $slider.find('ul')
				$ol = $('.img-slider > ol'), // $slider.find('ol')
				len = $ul.find('li').length,
				width = $ul.find('li').width(),
				ulWidth = len * width,
				timer;

			$ul.width(ulWidth);

			// for

			// 2
			// $slider.find('a')
			$('.img-slider > a').on('click', function() {
				var $this = $(this),
					currIndex = $ol.find('li.active').index();

				if ( $('ul:animated').length > 0 ) {
					return;
				}

				if ($this.hasClass('next')) {
					currIndex++;
					currIndex = (currIndex == len) ? 0 : currIndex;
					// (currIndex == len) && (currIndex = 0);
					/*if (currIndex == len) {
						currIndex = 0;
					}*/
				} else {
					currIndex--;
					if (currIndex < 0) {
						currIndex = len - 1;
					}
				}
				$ol.find('li').eq(currIndex).trigger('click');
				// console.log(currIndex)
			});

			// 1
			$ol.find('li').on('click', function() {
				var $this = $(this),
					index = $this.index();

				// $this.addClass('active').siblings('.active').removeClass('active');

				$ul.animate({marginLeft: -width * index}, 200, function() {
					$this.addClass('active').siblings('.active').removeClass('active');
				});
			});

			// 4
			$slider
				.on('mouseover', function() {
					clearInterval(timer);
				})
				.on('mouseout', function() {
					runner();
				})

			// 3
			function runner() {
				timer = setInterval(function() {
					$slider.find('a.prev').trigger('click');
					// $('.img-slider > a.next').trigger('click');
				}, 2000);
			}

			runner();

		});


	</script>
</head>
<body>

	<div class="img-slider">
		<ul>
			<li>item1</li>
			<li>item2</li>
			<li>item3</li>
			<li>item4</li>
			<li>item5</li>
			<li>item6</li>
		</ul>
		<a href="javascript:;" class="prev"></a>
		<a href="javascript:;" class="next"></a>
		<ol>
			<li class="active"></li>
			<li></li>
			<li></li>
			<li></li>
			<li></li>
			<li></li>
		</ol>
	</div>
	
</body>
</html>